/**
 * Created by sun on 2016/8/4.
 */
define(function (require) {
    var app = require('/../js/app');
    require('ui-table');

    var toastr =require('toastr');
    app.useModule("ui.table");


    var datepicker = require('datepicker');
    app.controller('guarantee_rechargeCtrl', ['$scope','$http','$filter' ,function ($scope, $http,$filter) {

        $scope.select2Options = {
            minimumResultsForSearch: Infinity //不出现搜索框
        };
        var scopePath = window.location.host;

        $scope.ctrlScope=$scope;
        $scope.add={};
        $scope.comp={};
        $scope.list={};
        $scope.rechargeStatus="";
        $scope.orderParams = {
            url: "/admin/accountrecharge/listByGuaranteeId"
        };
        datepicker($scope);

        //创建
        $scope.addOrderModal=function(){
            $scope.addModal = !$scope.addModal;
            $scope.add={};
            $scope.add.resource='平台充值';
        };

        //公司充值
        $scope.addCompanyModal=function(){
            $scope.companyModal = !$scope.companyModal;
            $scope.comp={};
            $scope.comp.resource='平台充值';
        };
        /*$scope.getShow=function(parm){
            $scope.show={};
            $http({
                method: 'POST',
                url: "/admin/accountrecharge/statRecharge",
                data:parm,
            }).success(function(data) {
                console.info(data);
                //$scope.data=data;
                $scope.show=data
            }).error(function(data) {});
        }*/
      /*  $scope.getShow();*/


        //手机号码是否注册
        $scope.isRegister= function (mobile) {
            var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
            if(!myreg.test(mobile)) {
                alert('请输入有效的手机号码！');
                return false;
            }
            $http({
                method: 'POST',
                url: "/admin/accountrecharge/mobileIsExist",
                data:{username:mobile}
            }).success(function(data) {
                console.info(data);
                if(data.result=='fail'){
                    toastr.error('用户不存在');
                }else{
                    toastr.success('验证成功');
                    $scope.add.realname=data.realname;
                }



            }).error(function(data) {});
        };
        //全局查询
        $scope.search = function() {
            $scope.ctrlScope.itemsearch.beginDateTime=$scope.ctrlScope.itemsearch.beginDateTimex;
            $scope.ctrlScope.itemsearch.endDateTime=$scope.ctrlScope.itemsearch.endDateTimex;
            $scope.ctrlScope.itemsearch.beginDateTime=$filter('date')($scope.ctrlScope.itemsearch.beginDateTime,'yyyy-MM-dd');
            $scope.ctrlScope.itemsearch.endDateTime=$filter('date')($scope.ctrlScope.itemsearch.endDateTime,'yyyy-MM-dd');
            //表格查询参数设置
            $scope.orderParams.ajaxParam=$scope.ctrlScope.itemsearch;
            var parm=$scope.ctrlScope.itemsearch;
            $scope.getShow(parm);
            //表格刷新
            $scope.orderParams.reload();
        };

        //全局查询重置
        $scope.reset=function(){
            $scope.ctrlScope.itemsearch={};
            $scope.orderParams.ajaxParam={};
            $scope.orderParams.reload();
            $scope.getShow();
        };

        //充值新建
        $scope.adoptAdd = function(isValid) {
            if (!isValid) {
                toastr.error("信息填写有误");
            }
            window.location.href='http://'+scopePath+'/admin/accountrecharge/platformRecharge?username='+
                $scope.add.username+"&newRechargeAmount="+$scope.add.newRechargeAmount+"&remark="+$scope.add.remark+
                "&type="+$scope.add.type;
        };
        //充值新建
        $scope.compRechargeAdd = function(isValid) {
            if (!isValid) {
                toastr.error("信息填写有误");
            }
            window.location.href='http://'+scopePath+'/admin/accountrecharge/platformRecharge?newRechargeAmount='
                +$scope.comp.newRechargeAmount+"&remark="+$scope.comp.remark+
                "&type=3";
        };
        //编辑
        $scope.modifyModal = function(orderNo){
            $scope.detailsModal= !$scope.detailsModal;
            $scope.getOne(orderNo);
        };
        //单个查询
        $scope.getOne = function(orderNo){
            $http({
                method: 'POST',
                url: "/admin/accountrecharge/getSingle",
                data: {
                    orderNo:orderNo
                }
            }).success(function(data) {
                console.info(data);
                $scope.list=data;
                $scope.rechargeStatus=data.rechargeStatus;
            }).error(function(data) {
            });
        };
        //保存
        $scope.saveSubmit = function(rechargeStatus){
            var parm={orderNo:$scope.list.orderNo,oldStatus:rechargeStatus,newStatus:$scope.list.rechargeStatus};
            console.info(parm);
            $http({
                method: 'POST',
                url: "/admin/accountrecharge/updateStatus",
                data:parm,
            }).success(function(data) {
                if(data.result=="success"){
                    toastr.success("保存成功");
                    $scope.detailsModal= !$scope.detailsModal;
                    $scope.orderParams.reload();
                }else{
                    toastr.error("保存失败");
                }

            }).error(function(data) {
            });
        };
        /*//导出
        $scope.download=function(){
            var params=$scope.ctrlScope.itemsearch;
            $http.post("/admin/accountrecharge/excel/accountRecharge",params)
                .success(function (data){
                    console.info(data);
                    if(data.result=="success"){
                        window.open(data.file);
                    }else{
                        toastr.error("导出失败");
                    }
                }).error(function(data){

                });
        };*/

    }]);


});